package five;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ConversionExample {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();

        // List<Integer> --> int[]
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        int[] ints = mapper.convertValue(list, int[].class);
        System.out.println(ints[0]);
        System.out.println(ints[ints.length - 1]);


        //pojo --> map
        Bean a = new Bean("A", 1);
        Map map = mapper.convertValue(a, Map.class);
        System.out.println(map.get("NAME"));
        System.out.println(map.get("AGE"));

//        map-- > pojo;
        Bean b = mapper.convertValue(map, Bean.class);
        System.out.println(b);


//        base64
        // decode Base64! (default byte[] representation is base64-encoded String)
        String base64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz";
        byte[] binary = mapper.convertValue(base64, byte[].class);
    }
}
